<template>
	<view class="root">


		<navigator class="hist-item" v-for="(item, index) in hist" :key="index" :url="item.url">
			<view class="hist-txt">
				<text>{{ item.title }}</text>
				<text>{{ new Date(item.time).toLocaleString() }}</text>
			</view>
			<image :src="item.img" mode="aspectFill"></image>
		</navigator>

		<view class="blank-list-placeholder" v-if="!hist.length">没有历史记录~ 去找点书看看吧?</view>

		<view class="topActions" v-else>
			<view class="clearBtn" @click="clearHist()">全部清除</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				hist: [{
					title: '',
					url: '',
					img: '',
					time: 0
				}],
			}
		},

		onLoad() {
			this.hist = []
		},

		onShow() {
			this.loadHist()
		},

		methods: {
			loadHist() {
				try {
					this.hist = JSON.parse(uni.getStorageSync('browsing_history'))
				} catch (e) {
					this.hist = []
				}
			},

			clearHist() {
				uni.showModal({
					title: '确认清除?',
					success: (res) => {
						if (res.confirm) {
							uni.removeStorageSync('browsing_history')
							this.loadHist()
						}
					}
				})
			},
		}
	}
</script>

<style>
	.root {
		display: flex;
		flex-flow: column-reverse;
	}

	.topActions {
		display: flex;
		flex-flow: column;
		background-color: #f4f4f4;
		padding: 8px;
	}

	.clearBtn {
		align-self: flex-end;
		font-size: 0.9em;
		color: #888;
	}

	.hist-item {
		background-color: white;
		display: flex;
		align-items: center;
		padding: 12px;
		border-bottom: 12px solid #f4f4f4;
	}

	.hist-txt {
		flex: 1;
		min-width: 0;
		display: flex;
		flex-flow: column;
	}

	.hist-txt>text {
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	.hist-txt>text:first-child {
		font-weight: bold;
		font-size: 1.1em;
	}

	.hist-txt>text:last-child {
		color: #888;
		font-size: 0.9em;
	}

	.hist-item>image {
		width: 70px;
		height: 70px;
	}
</style>